package com.hll.javaweb.servlet.json;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author hll
 * @create 2022-05-10 16:50
 */
@WebServlet("/json")
public class JSONTest extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ArrayList<User> users = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      users.add(new User("name", i));
    }
    resp.setCharacterEncoding("UTF-8");
    resp.getWriter().write(JSON.toJSONString(users));
  }
}
